package com.pingpang.common.code.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import com.pingpang.common.code.MessageCodecUtil;

public class HessianCodeUtil implements MessageCodecUtil {

	@Override
	public byte[] encode(Object message) throws IOException{
			 ByteArrayOutputStream os = new ByteArrayOutputStream();
		     HessianOutput ho = new HessianOutput(os);
		     ho.writeObject(message);  
		     byte[] data = os.toByteArray(); 
	         return data;
	}

	@Override
	public Object decode(byte[] body, Class cls) throws IOException {
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);
        HessianInput hi=new HessianInput(byteArrayInputStream);
        Object obj= hi.readObject(cls);
        return obj;
	}

}
